package com.xl.bigdata.es.index;

import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.support.replication.ReplicationResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;

/**
 * @ClassName DeleteAPI
 * @Description TODO
 * @Author xl.gao
 * @Date 2021/12/31 8:51
 * @Version
 */
public class DeleteAPI {

    /**
     * @Description:
     * @Author: xl.gao
     * @Date:
     * @param client:
     * @param index:
     * @param id:
     * @return: boolean
     **/
    public static boolean deleteAPI(RestHighLevelClient client, String index, String id){

        DeleteRequest deleteRequest = new DeleteRequest(index, id);

        try {
            DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
            ReplicationResponse.ShardInfo shardInfo = delete.getShardInfo();
            if(shardInfo.getFailed() > 0){
                return false;
            } else {
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }
}
